//
//  Network_Operation.m
//  AFNetworkingDemo
//
//  Created by xxcao on 14-4-24.
//  Copyright (c) 2014年 xxcao. All rights reserved.
//

#import "Network_Operation.h"
#import "AFDownloadRequestOperation.h"

static const double time_out_seconds = 20.0;

@implementation Network_Operation

+ (AFHTTPClient *)httpClientWithUrl:(NSString *)urlStr {
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlStr]];
//    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
//    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    return httpClient;
}


#pragma -mark
#pragma -mark POST, DELETE Request
+ (AFHTTPRequestOperation *)requestPostOperation_Url:(NSString *)urlString
                                          Parameters:(id)parameter
                                              Method:(NSString *)method {
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    
    NSMutableURLRequest *request = [httpClient requestWithMethod:method
                                                            path:urlString
                                                      parameters:parameter];
    [request setTimeoutInterval:time_out_seconds];
    return [[AFHTTPRequestOperation alloc] initWithRequest:request];
}

#pragma -mark
#pragma -mark GET Request
+ (AFHTTPRequestOperation *)requestGetOperation_Url:(NSString *)urlString {
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                            path:urlString
                                                      parameters:nil];
    [request setTimeoutInterval:time_out_seconds];
    return [[AFHTTPRequestOperation alloc] initWithRequest:request];
}

#pragma -mark
#pragma -mark Upload File Request
+ (AFHTTPRequestOperation *)uploadOperation_Url:(NSString *)url
                                           Body:(id)parameter
                                       FileName:(NSString *)fName
                                           Data:(id)data {
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    [httpClient registerHTTPOperationClass: [AFHTTPRequestOperation class]];
    
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                         path:url
                                                                   parameters:parameter
                                                    constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                                        [formData appendPartWithFileData:data
                                                                                    name:@"kala.png"
                                                                                fileName:fName
                                                                                mimeType:@"image/jpeg"];
                                                    }];
    [request setTimeoutInterval:time_out_seconds];
    return [[AFHTTPRequestOperation alloc] initWithRequest:request];
}

+ (AFHTTPRequestOperation *)downloadOperation_Url:(NSString *)url
                                         FilePath:(NSString *)fPath {
//    NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
//                                                     cachePolicy:NSURLRequestReloadIgnoringCacheData
//                                                 timeoutInterval:time_out_seconds];
//    
//    AFDownloadRequestOperation *downloadOp = [[AFDownloadRequestOperation alloc] initWithRequest:downloadRequest
//                                                                                      targetPath:fPath
//                                                                                    shouldResume:YES];
    
    NSMutableURLRequest *downloadRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    [downloadRequest setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
    [downloadRequest setHTTPMethod:@"POST"];
    
    AFHTTPRequestOperation *downloadMusicOP = [[AFHTTPRequestOperation alloc]initWithRequest:downloadRequest];
    [downloadMusicOP setOutputStream:[NSOutputStream outputStreamToFileAtPath:fPath append:NO]];
    return downloadMusicOP;
}

@end
